package com.luckku.boot.controller;


import com.luckku.boot.common.api.ApiResult;
import com.luckku.boot.entity.User;
import com.luckku.boot.entity.UserExam;
import com.luckku.boot.service.UserExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.luckku.boot.jwt.JwtUtil.USER_ID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Luckku
 * @since 2022-10-09
 */
@RestController
@RequestMapping("/userExam")
public class UserExamController {

    @Autowired
    UserExamService userExamService;

    /**
     * /userExam/register
     * 用户报名
     *
     * @param userExam UserExam
     * @return ApiResult<Map < String, Object>>
     */
    @PostMapping(value = "/register")
    public ApiResult<Map<String, Object>> register(@Valid @RequestBody UserExam userExam) {

        userExam = userExamService.executeRegister(userExam);
        if (!ObjectUtils.isEmpty(userExam.getAdmissionTicketId())) {
            Map<String, Object> map = new HashMap<>(16);
            map.put("userExam", userExam);
            return ApiResult.success(map);
        }

        return ApiResult.failed("注册考试至少需要提交userId、examId和siteId");

    }

    /**
     * /userExam/getById
     * 获取用户考试信息
     *
     * @param admissionTicketId String
     * @return ApiResult<UserExam>
     */
    @GetMapping(value = "/getById")
    public ApiResult<UserExam> getById(@RequestParam(value = "admissionTicketId") long admissionTicketId) {
        UserExam userExam = userExamService.getById(admissionTicketId);
        if (userExam != null) {
            return ApiResult.success(userExam);
        }
        return ApiResult.failed("not found");
    }

    /**
     * /userExam/getAll
     * 获取所有用户信息
     *
     * @return ApiResult<List < UserExam>>
     */
    @GetMapping(value = "/getAll")
    public ApiResult<List<UserExam>> getAll() {
        List<UserExam> userExamList = userExamService.list();
        if (userExamList != null) {
            return ApiResult.success(userExamList);
        }
        return ApiResult.failed("userExamList not found");
    }


    /**
     * /userExam/update
     * 修改用户考试信息
     *
     * @return ApiResult<UserExam>
     */
    @PutMapping(value = "/update")
    public ApiResult<UserExam> update(@RequestBody UserExam userExam) {
        userExamService.updateById(userExam);
        return ApiResult.success(userExam);
    }

}

